<template>
	<view class="content">

		<image style="width:100%;position: absolute;bottom:0;left:0;width:100%;" mode="widthFix"
			src="../../static/1.svg"></image>

		<view class="loginBox">
			<h3 style="text-align: center;margin-bottom:120rpx;">欢迎注册智学博客</h3>
			<view class="inputBox">
				<view class="ipt">
					<uni-icons type="contact" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="text" value="" v-model="user.username" placeholder="请输入用户名" />
				</view>
				<view class="ipt">
					<uni-icons type="eye" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="password" value="" v-model="user.password" placeholder="请输入密码" />
				</view>
				<view class="ipt">
					<uni-icons type="eye" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="number" id="phoneNumber" pattern="\d{11}" maxlength="11" inputmode="numeric" v-model="user.phoneNum" placeholder="请输入手机号" required>
				</view>
				<view class="ipt">
					<uni-icons type="eye" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="text" value="" v-model="user.userEmail" placeholder="请输入邮箱" />
				</view>
				<button @click="handleRegister">注册</button>

			</view>
			<view class="tipbox">

			</view>
		</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {
				// 定义一个 user 对象来保存用户名和密码
				user: {
					username: '',
					password: '',
					phoneNum: '',
					userEmail: ''
				}
			};
		},
		methods: {
			async handleRegister() {
				// 检查输入是否为空
				if (!this.user.username) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				} else if (!this.user.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
				} else if (!this.user.phoneNum) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
				} else if (!this.user.userEmail) {
					uni.showToast({
						title: '请输入邮箱',
						icon: 'none'
					});
				}

				try {
					// 发送注册请求
					const res = await uni.request({
						url: 'http://localhost:9091/font/client/register',
						method: 'POST',
						data: this.user,
						header: {
							'Content-Type': 'application/json' // POST 数据内容类型
						}
					});
					console.log("后端响应:", res);
					// 处理服务器返回的数据
					if (res.data.code == 2004) {
						// 如果状态码为2004，则表示注册成功
						uni.showToast({
							title: '注册成功',
							duration: 2000
						});
						// 进行其他操作，例如跳转页面等
						// uni.navigateTo({
						// 	url: "/pages/login/index",
						// })
						window.location.href = "/#/pages/home/home";
					} else if (res.data.code == 5001) {
						uni.clearStorageSync()
						uni.showToast({
							title: '注册失败',
							duration: 2000,
							icon: "error",
						})
					} else if (res.data.code == 5003) {
						uni.showToast({
							title: '当前手机号已被注册',
							duration: 2000,
							icon: "error",
						})
					}
				} catch (error) {
					console.error('注册请求错误:', error);
					if (error.errMsg.indexOf('request:fail') !== -1) {
						wx.showToast({
							title: '网络异常',
							icon: "error",
							duration: 2000
						})
					} else {
						wx.showToast({
							title: '未知异常',
							duration: 2000
						})
					}
				}
			},
		}
	};

	
</script>

<style scoped>
	svg {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 40%;
		box-sizing: border-box;
		display: block;
		background-color: #ffffff;
	}

	.loginBox {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -60%);
		width: 90%;
		border-radius: 20rpx;
		padding: 60rpx;
		box-sizing: border-box;
	}

	h3 {
		color: rgb(66, 157, 250);
		font-size: 40rpx;
		letter-spacing: 10rpx;
		margin-bottom: 40rpx;
	}

	.inputBox {}

	.ipt {
		height: 86rpx;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin-bottom: 40rpx;
		background-color: #f5f5f5;
		border-radius: 10rpx;
		padding-left: 10rpx;
	}

	.ipt input {
		margin-left: 20rpx;
		font-size: 28rpx;
	}

	.ipt input {
		margin-left: 20rpx;
	}

	.forgetPwd {
		margin-top: 30rpx;
		font-size: 26rpx;
		color: #b5b5b5;
		text-align: end;
		padding: 0 10rpx;
		display: flex;
		justify-content: space-between;
	}

	button {
		margin-top: 20rpx;
		line-height: 85rpx;
		text-align: center;
		background: rgb(66, 157, 250);
		border-radius: 40rpx;
		color: #fff;
		margin-top: 40rpx;
	}

	.tip {
		text-align: center;
		font-size: 28rpx;
		position: fixed;
		bottom: 50rpx;
		left: 50%;
		transform: translate(-50%, -50%);
		color: #f4f4f4;
	}

	.tipbox {
		text-align: center;
		margin-top: 100rpx;
	}

	.otherUser {
		margin-top: 30rpx;
		display: flex;
		justify-content: center;
	}

	.txt {
		font-size: 28rpx;
		color: #cbcbcb;
	}

	.otherUser .uni-icons {
		margin-left: 20rpx;
	}

	.yzm {
		text-align: end;
		font-size: 24rpx;
		background: linear-gradient(to right, rgb(66, 157, 250), rgb(0, 170, 127));
		height: 60rpx;
		width: 150rpx;
		line-height: 60rpx;
		text-align: center;
		border-radius: 10rpx;
		color: #fff;
	}
</style>